package com.hengpeng.itfinoss.controller.bankproduct;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.BankProduct;
import com.hengpeng.itfinbase.persist.BankProductContacts;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.util.PageHandlerUtil;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * <pre>
 * 战略联盟银行产品
 * </pre>
 * @author weiyuan
 * @version 1.0, 2015年8月23日
 */
@Controller
@RequestMapping("/bankProduct")
public class BankProductController extends BaseController
{
	public final static Log logger = LogFactory.getLog(BankProductController.class);

	@Autowired
	private ServiceFactory serviceFactory;

	private final static ExecutorService execute = Executors.newCachedThreadPool();

	@PreDestroy
	public void release()
	{
		if (execute != null)
		{
			execute.shutdown();
		}
	}

	/**
	 * <pre>
	 * 战略联盟银行产品列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param borrowName
	 * @param borrowUserName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/productlist")
	public String list(ModelMap map, HttpServletRequest request, String name)
	{
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		ServiceResult<List<BankProduct>> service = serviceFactory.getBankProductService().queryList(name, page.currentPage,
				page.pageSize);
		page.setEntityCount(service.getReturnListSize());
		map.put("bankProductList", service.getReturnValue());
		map.put("page", page);
		map.put("name", name);
		return "bankproduct/productlist";
	}
	/**
	 * 认购意向列表
	 * @param map
	 * @param request
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/buylist/{id}")
	public String buylist(ModelMap map, HttpServletRequest request,@PathVariable Long id)
	{
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		ServiceResult<List<BankProductContacts>> service = serviceFactory.getBankProductService().queryContactsList(id,page.currentPage,
				page.pageSize);
		page.setEntityCount(service.getReturnListSize());
		map.put("buylist", service.getReturnValue());
		map.put("page", page);
		return "bankproduct/buylist";
	}

	/**
	 * <pre>
	 * 转向产品新增
	 * </pre>
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String preCreate(ModelMap map)
	{
		BankProduct bankProduct = new BankProduct();
		map.put("bankproduct", bankProduct);
		return "bankproduct/addbankproduct";
	}

	/**
	 * <pre>
	 * 新增产品
	 * </pre>
	 * @param request
	 * @param borrow
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, BankProduct bankProduct, ModelMap map)
	{
		try
		{
			bankProduct.setUpdateTime(new Date());
			bankProduct.setCreateTime(new Date());
			serviceFactory.getPersistService().save(bankProduct);
			return PageHandlerUtil.returnSuccess(map, "新增成功", "/bankProduct/productlist");
		}
		catch (Exception ex)
		{
			logger.error(ex);
			return PageHandlerUtil.returnError(map, ex);
		}
	}

	@RequestMapping(value = "/get/{id}")
	public String get(@PathVariable Long id, ModelMap map)
	{
		BankProduct obj = serviceFactory.getPersistService().get(BankProduct.class, id);
		map.put("bankProduct", obj);
		return "bankproduct/updatebankproduct";
	}

	/**
	 * <pre>
	 * 修改产品
	 * </pre>
	 * @param request
	 * @param vo
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public String modify(HttpServletRequest request, BankProduct bankProduct, ModelMap map)
	{
		bankProduct.setUpdateTime(new Date());
		serviceFactory.getPersistService().update(bankProduct);
		return PageHandlerUtil.returnSuccess(map, "修改成功", "/bankProduct/productlist");
	}
}
